/*
============================================================================
 Name        : HandshakeForm.cpp
 Author      : 
 Version     :
 Copyright   : Your copyright notice
 Description : CHandshakeForm implementation
============================================================================
*/

#include "HandshakeForm.h"
#include "LastFmPlugin.rsg"
#include "LastFmPlugin.hrh"

#include <eikseced.h>	//CEikSecretEditor
#include <eikcapc.h>	//CEikCaptionedControl (in Line() function)
#include <eikmenup.h>	//CEikMenuPane
#include <eikedwin.h>	//CEikEdwin
#include <avkon.rsg>	//R_AVKON_FORM_MENUPANE

CHandshakeForm::CHandshakeForm(TDes& aUsername, TDes& aPassword)
: iUsername(aUsername), iPassword(aPassword)
{
}

CHandshakeForm::~CHandshakeForm()
{
}

CHandshakeForm* CHandshakeForm::NewLC(TDes& aUsername, TDes& aPassword)
{
	CHandshakeForm* self = new (ELeave)CHandshakeForm(aUsername, aPassword);
	CleanupStack::PushL(self);
	self->ConstructL();
	return self;
}

CHandshakeForm* CHandshakeForm::NewL(TDes& aUsername, TDes& aPassword)
{
	CHandshakeForm* self = CHandshakeForm::NewLC(aUsername, aPassword);
	CleanupStack::Pop(); // self;
	return self;
}

void CHandshakeForm::ConstructL()
{
	//Base construct
    CAknForm::ConstructL();

	//Prepare form
	CAknForm::PrepareLC(R_HANDSHAKE_FORM_DIALOG);

	//Set username text
	CEikEdwin* usernameEdwin = static_cast<CEikEdwin*>(ControlOrNull(ELastFmFormUsername));
    if (usernameEdwin)
	{
		usernameEdwin->SetTextL(&iUsername);
	}
}

TBool CHandshakeForm::SaveFormDataL()
{
	//Save username
	if (ControlOrNull(ELastFmFormUsername)) GetEdwinText(iUsername, ELastFmFormUsername);

	//Save password
    CEikSecretEditor* secretEd = (CEikSecretEditor*) ControlOrNull(ELastFmFormPassword);
    if (secretEd) secretEd->GetText(iPassword);

	return ETrue;
}

void CHandshakeForm::PostLayoutDynInitL()
{
	CAknForm::PostLayoutDynInitL();
}

TBool CHandshakeForm::QuerySaveChangesL()
{
	TBool isAnsYes(CAknForm::QuerySaveChangesL());
	if (isAnsYes)
	{
		SaveFormDataL();
	}
	else 
	{
		//Case that answer "No" to query.
		DoNotSaveFormDataL();
	}
	return isAnsYes;
}

void CHandshakeForm::DoNotSaveFormDataL()
{
}

void CHandshakeForm::DynInitMenuPaneL(TInt aResourceId, CEikMenuPane *aMenuPane)
{
	if (aResourceId == R_AVKON_FORM_MENUPANE)
	{
		//Disable add and delete label menu options
		aMenuPane->SetItemDimmed(EAknFormCmdAdd, ETrue);
		aMenuPane->SetItemDimmed(EAknFormCmdLabel, ETrue);
		aMenuPane->SetItemDimmed(EAknFormCmdDelete, ETrue);
	}
}

TInt CHandshakeForm::ExecuteLD()
{
	return CAknForm::RunLD();
}